package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 使用指定的构造器实例化对象
 * @author XingHaiYang
 * @date 2022/9/16 10:24
 */
public class ReflectDemo3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Person p = new Person("李四",33);
        System.out.println(p);
        Class cls = Class.forName("reflect.Person");//获取类对象
        Constructor c = cls.getConstructor(String.class, int.class);//通过类对象获取有参构造器
        Object obj = c.newInstance("传奇", 28);//通过有参构造器的newInstance获取对象实例
        System.out.println(obj);
    }
}
